﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindow.Enums;
using UIWindow.Helper;

namespace UIWindow.Protocols.Fins
{
    public class FinsRead : OmRonFinsBase
    {
        /// <summary>
        /// 本网络 00 远程网络 01-7F
        /// </summary>
        public byte DNA { get; set; } = 0x00;

        /// <summary>
        /// 服务器节点（握手包返回）
        /// </summary>
        public byte DA1 { get; set; }

        /// <summary>
        /// PLC单元号
        /// </summary>
        public byte DA2 { get; set; } = 0x00;

        /// <summary>
        /// 本网络 00
        /// </summary>
        public byte SNA { get; set; } = 0x00;

        /// <summary>
        /// IP地址最后一位
        /// </summary>
        public byte SA1 { get; set; }

        /// <summary>
        /// 源单元号 与目标一致
        /// </summary>
        public byte SA2 { get; set; } = 0x00;

        /// <summary>
        /// 操作区域
        /// </summary>
        public OmronOperatingAreaType OperatingArea { get; set; }

        /// <summary>
        /// 位地址（相当于偏移）
        /// </summary>
        public byte ByteAddress { get; set; } = 0;

        /// <summary>
        /// 读写地址
        /// </summary>
        public ushort Address { get; set; }

        /// <summary>
        /// 读写长度
        /// </summary>
        public ushort ProtoLength { get; set; }
        public FinsRead() : base()
        {
        }
        public override byte[] GetStream()
        {
            //发送数据长度：命令码开始的字节数量
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x1a);

            //命令码
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x02);

            //错误代码
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x00);

            //Fins
            base._memoryStream.WriteByte(0x80);
            base._memoryStream.WriteByte(0x00);
            base._memoryStream.WriteByte(0x02);
            base._memoryStream.WriteByte(DNA);
            base._memoryStream.WriteByte(DA1);
            base._memoryStream.WriteByte(DA2);
            base._memoryStream.WriteByte(SNA);
            base._memoryStream.WriteByte(SA1);
            base._memoryStream.WriteByte(SA2);
            base._memoryStream.WriteByte(0xFF);//SID固定

            //0x01 0x01 读取
            base._memoryStream.WriteByte(0x01);
            base._memoryStream.WriteByte(0x01);

            #region 四字节====190页
            //操作区域
            base._memoryStream.WriteByte((byte)OperatingArea);
            //起始地址
            base._memoryStream.Write(Address.ToString("X4").StrToHexByte(), 0, 2);
            //偏移
            base._memoryStream.WriteByte(ByteAddress);
            #endregion

            //读数量
            base._memoryStream.Write(ProtoLength.ToString("X4").StrToHexByte(), 0, 2);
            return base.GetStream();
        }
    }
}
